//
//  HQExcempleViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/10.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQExcempleViewController.h"

@interface HQExcempleViewController ()

@end

@implementation HQExcempleViewController

/**
 搜索框触发的方法
 
 @param searchBar 搜索框
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    if([self.pushType isEqualToString:@"店内"]){
        // 我的案例搜索
        if(searchBar.text.length == 0){
            [self requestShopData];
        }else{
            // 店内搜索
            _collectionView.backgroundView = nil;
            [self showHudText:@""];
            [HQNetWork requetParam:[HQParam WorksFindUserId:[UserModelManager userID] withKey:searchBar.text] withMethod:[HQMethod WorksFind] suc:^(NSDictionary *sucData) {
                [self hiddnHud];
                if([sucData[kStatus]integerValue] == 1){
                    _dagaSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
                    if(_dagaSource.count ==0){
                        [self addNoneDataCollView:_collectionView withText:@"搜索结果为空"];
                    }
                    [_collectionView reloadData];
                }else{
                    [self tostView:sucData[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }
    }else if([self.pushType isEqualToString:@"我的案例"]){
        // 我的案例搜索
        if(searchBar.text.length == 0){
            [self requestData];
        }else{
            // 店内搜索
            _collectionView.backgroundView = nil;
            [self showHudText:@""];
            [HQNetWork requetParam:[HQParam anliSearchPlotName:searchBar.text] withMethod:[HQMethod homeFinc] suc:^(NSDictionary *sucData) {
                [self hiddnHud];
                if([sucData[kStatus]integerValue] == 1){
                    _dagaSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
                    if(_dagaSource.count ==0){
                        [self addNoneDataCollView:_collectionView withText:@"搜索数据为空"];
                    }
                    [_collectionView reloadData];
                }else{
                    [self tostView:sucData[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if([self.pushType isEqualToString:@"店内"]){
        [self addSearchBar:@"搜索货号、风格和厂家"];
        [self requestShopData];
    }else if([self.pushType isEqualToString:@"我的案例"]){
        [self requestData];
        [self addSearchBar:@"搜索小区"];
    }else if([self.pushType isEqualToString:@"热点商品"]){
        _addButton.hidden = YES;
    }else if([self.pushType isEqualToString:@"新品专区"]){
        _addButton.hidden = YES;
    }
    
    //创建瀑布流布局
    XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
    
    //或者一次性设置
    [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    waterfall.delegate = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"HQShopCollCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    _collectionView.collectionViewLayout = waterfall;
}

//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
    //根据图片的原始尺寸，及显示宽度，等比例缩放来计算显示高度
    //    XRImage *image =  self.images[indexPath.item];
    
    XRImage *image = [XRImage imageWithImageDic:@{@"img":@"http://img.zcool.cn/community/010f87596f13e6a8012193a363df45.jpg@1280w_1l_2o_100sh.jpg",@"itemIndex":[NSNumber numberWithInteger:indexPath.row]}] ;
    return image.imageH / image.imageW * itemWidth;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dagaSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HQShopCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if([self.pushType isEqualToString:@"店内"]){
        cell.imageURL = [NSURL URLWithString:_dagaSource[indexPath.row][@"w_img"]];
        cell.itemLabel.text = _dagaSource[indexPath.row][@"w_style"];
    }else if([self.pushType isEqualToString:@"我的案例"]){
        [cell.itemImage setImageWithURL:[NSURL URLWithString:_dagaSource[indexPath.row][@"case_img"]] placeholderImage:[UIImage imageNamed:@"none_image"]];
        cell.itemLabel.text = _dagaSource[indexPath.row][@"plot_name"];
        cell.userName.text = _dagaSource[indexPath.row][@"case_name"];
    }
    return cell;
}

#pragma mark 选中某行触发的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if([self.pushType isEqualToString:@"店内"]){
        HQShopDetailViewController *shopDetail = [[HQShopDetailViewController alloc]init];
        shopDetail.dataInfo = _dagaSource[indexPath.row];
        [self.navigationController pushViewController:shopDetail animated:YES];
    }else if([self.pushType isEqualToString:@"我的案例"]){
        HQExcempleDetailViewController *exCempleUploadVc = [[HQExcempleDetailViewController alloc]init];
        exCempleUploadVc.dataInfo = _dagaSource[indexPath.row];
        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
    }
}

/**
 添加样式
 
 @param sender test
 */
- (IBAction)addStyle:(id)sender {
    if([self.pushType isEqualToString:@"店内"]){
        HQShopUploadViewController *exCempleUploadVc = [[HQShopUploadViewController alloc]init];
        exCempleUploadVc.delegate = self;
        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
    }else if([self.pushType isEqualToString:@"我的案例"]){
        HQExcempleUploadViewController *exCempleUploadVc = [[HQExcempleUploadViewController alloc]init];
        exCempleUploadVc.delegate = self;
        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
    }
}

/**
 刷新样式列表数据
 */
-(void)reloadShopData{
    NSLog(@"刷新店内样式数据");
    [self requestShopData];
}

/**
 请求店内
 */
-(void)requestShopData{
    _collectionView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam worksFindWorksWithUID:[UserModelManager userID]] withMethod:[HQMethod worksFindWorks] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dagaSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dagaSource.count ==0){
                [self addNoneDataCollView:_collectionView withText:@"店内样式暂无数据,请您添加样式"];
            }
            [_collectionView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 刷新我的样式数据
 */
- (void)reloadMyExcempleData{
    NSLog(@"刷新我的样式数据");
    [self requestData];
}

/**
 请求案例接口
 */
-(void)requestData{
    _collectionView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam allComponents:[UserModelManager userID]] withMethod:[HQMethod myCase] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dagaSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dagaSource.count ==0){
                [self addNoneDataCollView:_collectionView withText:@"我的案例暂无数据,请您添加案例"];
            }
            [_collectionView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
